﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;
using System.Collections;
using IBatisNet.Extend;

namespace ExpertMIS.Admin.UserControl
{
    public partial class QuestionsForm : System.Web.UI.UserControl
    {
        #region 控件事件处理

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gvAnswer.TmpColVisible = false;
                gvAnswer.ObjectName = "ExpertMIS.AnswerListView";

                RefreshExpertUsers();
                RefreshIdiom();
            }
        }

        /// <summary>
        /// 更新专家用户列表
        /// </summary>
        private void RefreshExpertUsers()
        {
            ADMUserBroker broker = new ADMUserBroker();
            ddlExpert.DataSource = broker.GetAllExpertUsers();
            ddlExpert.DataTextField = "Name";
            ddlExpert.DataValueField = "OID";
            ddlExpert.DataBind();
        }

        protected void ddlIdiom_SelectedIndexChanged(object sender, EventArgs e)
        {
            txAContent.Text = ddlIdiom.SelectedValue;
        }

        #endregion

        #region 公有方法


        public QuestionEntity Entity
        {
            get
            {
                return ViewState["_QuestionEntity_"] as QuestionEntity;
            }
            set
            {
                ViewState["_QuestionEntity_"] = value;
            }
        }

        public void LoadQuestion(string questionOID)
        {
            ADMUserBroker userBroker = new ADMUserBroker();
            DomainBroker domainBroker = new DomainBroker();
            //HACK，wdong
            QuestionBroker broker = new QuestionBroker();
            Hashtable htQuery = new Hashtable();
            htQuery["OID"] = questionOID;
            Entity = broker.GetEntity(htQuery) as QuestionEntity;

            if (Entity == null)
            {
                Entity = broker.NewEntity() as QuestionEntity;
            }
            else
            {
                ADMUserEntity user = userBroker.GetEntityByOID(Entity.UserOID);
                if (user != null)
                {
                    lblQuestionUser.Text = user.Name;
                }
                else
                {
                    lblQuestionUser.Text = "";
                }

                IList<DomainEntity> domainList = domainBroker.GetDomainsByQuerytionOID(Entity.OID);
                if (domainList != null && domainList.Count > 0)
                {
                    string domains = "";
                    foreach (DomainEntity e in domainList)
                    {
                        if (domains.Length > 0)
                        {
                            domains += ",";
                        }
                        domains += e.DomainName;
                    }

                    lblQuestionDomain.Text = domains;
                }
                else
                {
                    lblQuestionDomain.Text = "";
                }


                if (!string.IsNullOrEmpty(Entity.Title) && Entity.Title.Length > 0)
                {
                    lblQuestionTitle.Text = Entity.Title;
                }
                else
                {
                    lblQuestionTitle.Text = "";
                }

                txQContent.Text = Entity.Context;

                // 判断当前问题如果已审核过，则不可以重复审核
                if (Entity.Status == QuestionStatus.UnPassed)
                {
                    rblIsPassed.Enabled = true;
                }
                else
                {
                    rblIsPassed.Enabled = false;
                }

                rblIsPassed.SelectedValue = Entity.Status == QuestionStatus.UnPassed? "false" : "true";
            }
        }

        public bool SaveQuestion()
        {
            try
            {
                //HACK,wdong
                QuestionBroker qBroker = new QuestionBroker();

                // 判断当前问题如果已审核过，则不可以重复审核
                if (rblIsPassed.Enabled)
                {
                    Entity.Status = bool.Parse(rblIsPassed.SelectedValue) ? QuestionStatus.Unsolve : QuestionStatus.UnPassed;
                }

                ErrorInfo result = qBroker.SaveEntity(Entity, null);
                if (result.Code == 0)
                {
                    // wdong@2009-09-12,判断专家回复内容是否为空，为空则不保存回复
                    if (string.IsNullOrEmpty(txAContent.Text) || txAContent.Text.Trim().Length == 0)
                    {
                        return true;
                    }
                }
                else
                {
                    return false;
                }

                AnswerBroker aBroker = new AnswerBroker();
                AnswerEntity answerEntity = new AnswerEntity();
                answerEntity.UserOID = ddlExpert.SelectedValue;
                answerEntity.Context = txAContent.Text;
                answerEntity.QuestionOID = Entity.OID;
                answerEntity.Status = AnswerStatus.Normal;
                answerEntity.UpdateDate = DateTime.Now;
                answerEntity.ClientIP = Request.UserHostAddress;
                aBroker.SaveEntity(answerEntity, null);
               
                return true;
            }
            catch
            {
                // 显示错误信息
                return false;
            }
        }

        /// <summary>
        /// 刷新惯用语
        /// </summary>
        public void RefreshIdiom()
        {
            //HACK,wdong
            ADMIDIOMBroker broker = new ADMIDIOMBroker();
            IList<ADMIDIOMEntity> list = broker.GetAllIdioms();

            if (list != null && list.Count > 0)
            {
                ADMIDIOMEntity entity = new ADMIDIOMEntity();
                entity.OID = null;
                entity.Content = "";
                list.Insert(0, entity);
                ddlIdiom.DataSource = list;
                ddlIdiom.DataTextField = "Content";
                ddlIdiom.DataValueField = "Content";
                ddlIdiom.DataBind();
            }
        }

        #endregion
    }
}